package com.atos.howl.chaos.web.config;

import com.atos.howl.chaos.web.interceptor.TokenInterceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Howl-Xu
 * @version 1.0.0
 * @Description: 不拦截登陆请求
 * @createTime 2020年02月07日 18:54:00
 */

@Configuration
public class InterceptorConfig extends WebMvcConfigurationSupport{
    private TokenInterceptor tokenInterceptor;

    public InterceptorConfig(TokenInterceptor tokenInterceptor) {
        this.tokenInterceptor = tokenInterceptor;
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        List<String> excludePath = new ArrayList<>();
        //排除掉登陆和注册请求
        String sysUserLogin = "/api/loginController/login";
        String sysUserRegister = "/api/registerController/register";
        //排除掉下載路徑
        String downloadURL = "/api/downloadController/download/*";
        //排除掉排行榜路径
        String paihangbangURL = "/api/paihangbang/*";
        //排除掉弹幕发射路径，项目里默认谁都可以对任何视频发送弹幕，不会记录弹幕发射人的信息
        String danmuShootURL = "/api/danmuController/*";
        excludePath.add(sysUserLogin);
        excludePath.add(sysUserRegister);
        excludePath.add(downloadURL);
        excludePath.add(paihangbangURL);
        excludePath.add(danmuShootURL);
        registry.addInterceptor(tokenInterceptor).excludePathPatterns(excludePath);
    }

    //在这个配置里同时添加cors支持。
    @Override
    public void addCorsMappings(CorsRegistry registry)
    {
        // 设置允许跨域的路径
        registry.addMapping("/**")
                // 设置允许跨域请求的域名
                .allowedOrigins("*")
                // 是否允许证书
                .allowCredentials(true)
                // 设置允许的方法
                .allowedMethods("GET", "POST", "DELETE", "PUT")
//                .allowedMethods("GET", "POST", "DELETE", "PUT","OPTIONS")
                // 设置允许的header属性
                .allowedHeaders("*")
                // 跨域允许时间
                .maxAge(3600);
    }
}